/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Text;
using Shuriken.Core.Store; 
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Shuriken.Core.Ranking
{
    /// <summary>
    /// Implements simple rank provider using regular expressions
    /// </summary>
    /// <remarks>
    /// This provider matches wors only
    /// </remarks>
    public class SimpleRankProvider : IRankProvider
    {
        #region IRankProvider Members

        /// <summary>
        /// Calculates rank score for a given expression and candidate
        /// </summary>
        /// <param name="expression">search expression</param>
        /// <param name="candidate">candidate for this search</param>
        /// <param name="hitMask">hitmask for this search</param>
        /// <returns>score from 0 to 99, the higher the value is the better</returns>
        public double CalculateRank(ISearchable candidate, string expression, Shuriken.Core.Ranking.MutableIndexSet hitMask)
        {
            if (candidate == null) return 0;
            return CalculateRank(candidate.Name, expression, hitMask);
        }

        /// <summary>
        /// Calculates rank score for a given expression and candidate
        /// </summary>
        /// <param name="expression">search expression</param>
        /// <param name="candidate">candidate for this search</param>
        /// <returns>score from 0 to 99, the higher the value is the better</returns>
        public double CalculateRank(string candidate, string expression)
        {
            if (expression == null || candidate == null)
            {
                throw new NullReferenceException("Null Parameters passed!");
            }

            if (expression.Length < 1)
            {
                return 0;
            }

            if (candidate.Length < 1)
            {
                return 0;
            }

            Regex reg = new Regex(@"\w*" + expression + @"\w*", RegexOptions.IgnoreCase);

            if (reg.IsMatch(candidate))
            {
                return 50;
            }

            return 0;
        }

        /// <summary>
        /// Calculates rank score for a given expression and candidate
        /// </summary>
        /// <param name="expression">search expression</param>
        /// <param name="candidate">candidate for this search</param>
        /// <param name="hitMask">hitmask for the search, it will return an empty hitmask</param>
        /// <returns>score from 0 to 99, the higher the value is the better</returns>
        public double CalculateRank(string candidate, string expression, Shuriken.Core.Ranking.MutableIndexSet hitMask)
        {
            hitMask = new MutableIndexSet();
            return CalculateRank(candidate, expression);
        }


        public void RegisterHit(ISearchable candidate)
        {
            return;
        }

        #endregion
    }
}
